package hms.date;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;

public abstract class DateHandler implements ActionListener, FocusListener {
	protected JComboBox month;
	protected JComboBox day;
	protected JFormattedTextField year;
	
	static final String numbers = "0123456789";
	
	public DateHandler(JComboBox month, JComboBox day, JFormattedTextField year) {
		this.month = month;
		this.day = day;
		this.year = year;
		reset();
		month.addActionListener( this );
		year.addFocusListener( this );
		day.addActionListener( this );
		//year.getDocument().addDocumentListener( this );
	}
	
	protected int getDay() {
		return day.getSelectedIndex()+1;
	}
	
	protected void setDay(int day) {
		this.day.setSelectedIndex(day-1);
	}
	
	public int getMonth() {
		return month.getSelectedIndex()+1;
	}
	
	protected void setMonth(int month) {
		this.month.setSelectedIndex(month-1);
	}
	
	protected int getYear() {
		return Integer.parseInt(year.getText());
	}
	
	protected void setYear(int year) {
		this.year.setText(Integer.toString(year));
	}
	
	public void reset() {
		Calendar time = new GregorianCalendar();
		setDate(time);
	}
	
	
	public Calendar getDate() {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.MONTH,getMonth());
		c.set(Calendar.YEAR, getYear());
		c.set(Calendar.DAY_OF_MONTH, getDay());
		return c;
	}
	
	public void setDate(Calendar c) {
		setMonth(c.get(Calendar.MONTH)+1);
		setYear(c.get(Calendar.YEAR));
		adjustDays();
		setDay(c.get(Calendar.DAY_OF_MONTH));
	}
	
	public void setDate(int month, int day, int year) {
		setMonth(month);
		setYear(year);
		adjustDays();
		setDay(day);
	}
	
	public void setEnabled(boolean b) {
		month.setEnabled(b);
		day.setEnabled(b);
		year.setEnabled(b);
	}

	protected void adjustDays() {

		Calendar c = new GregorianCalendar();
		c.set(Calendar.MONTH, month.getSelectedIndex());
		c.set(Calendar.YEAR, Integer.parseInt(year.getText()));
		int index = day.getSelectedIndex();
		int daysInMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		day.removeAllItems();
		for(Integer i = 1; i<=daysInMonth; i++) {
			day.addItem(i);
		}
		if(index<daysInMonth) {
			day.setSelectedIndex(index);
		} else {
			day.setSelectedIndex(daysInMonth-1);
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		adjustDays();
		if(day.getSelectedIndex() > 0) {
			writeOut(getYear(), getMonth(), getDay());
		}
	}

	@Override
	public void focusGained(FocusEvent e) {
		
	}

	@Override
	public void focusLost(FocusEvent e) {
		adjustDays();
		if(day.getSelectedIndex() > 0) {
			writeOut(getYear(), getMonth(), getDay());
		}
	}

	abstract public void writeOut(int year, int month, int day);
}
